import 'package:flutter/material.dart';
import 'package:flutter_common_app/data/play_song_data.dart';
import 'package:flutter_common_app/data/song_detail_data.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/utils/common_util/date_util.dart';
import 'package:flutter_common_app/widgets/app_bar/custom_app_bar.dart';
import 'package:flutter_common_app/widgets/play_all_songs_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class RecommendDailySongsSliverHeader extends SliverPersistentHeaderDelegate {
  final List<SongDetailData> songsList;

  RecommendDailySongsSliverHeader(this.songsList);

  double _bottomRowItemH = 90.w;

  double minHeight = ScreenUtil().statusBarHeight + kToolbarHeight;

  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return SizedBox(
      width: double.infinity.w,
      height: maxExtent,
      child: Stack(
        fit: StackFit.expand,
        children: [
          Image.asset(
            'images/recommend_daily_header.jpg',
            width: 750.w,
            height: 250.w,
            fit: BoxFit.cover,
          ),
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: CustomAppBar(
              Text(
                '每日推荐',
                style: TextStyle(
                  fontSize: 32.sp,
                  color: Colors.white,
                ),
              ),
              leading: BackButton(
                color: Colors.white,
              ),
              actions: [
                Container(
                  child: Text(
                    '问题',
                    style: TextStyle(
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w400,
                      color: Colors.white,
                    ),
                  ),
                  height: kToolbarHeight,
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(right: 30.w),
                )
              ],
              backgroundColor: Colors.transparent,
            ),
          ),
          Positioned(
            left: 50.w,
            bottom: _bottomRowItemH + 30.w,
            child: Opacity(
                opacity: (1 - shrinkOffset / maxExtent) > 0.5
                    ? (1 - shrinkOffset / maxExtent)
                    : 0,
                child: Column(
                  children: [
                    Text.rich(
                      TextSpan(
                        children: [
                          TextSpan(
                            text: DateTime.now().day.toString(),
                            style: TextStyle(
                              fontSize: 70.sp,
                            ),
                          ),
                          TextSpan(text: ' / '),
                          TextSpan(
                              text: DateUtil.formatDate(DateTime.now(),
                                  format: 'MM')),
                        ],
                      ),
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.w600,
                        fontSize: 30.sp,
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 5.w),
                      padding:
                          EdgeInsets.symmetric(vertical: 5.w, horizontal: 15.w),
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.8),
                        borderRadius: BorderRadius.all(
                          Radius.circular(1000),
                        ),
                      ),
                      child: Text(
                        '历史推荐',
                        style: TextStyle(fontSize: 24.sp),
                      ),
                    ),
                  ],
                )),
          ),
          Positioned(
            bottom: 0,
            child: GestureDetector(
              onTap: () {
                Provider.of<AudioModel>(context, listen: false)
                    .playAll(songsList.map((item) {
                  return PlaySongData(
                    item.id,
                    name: item.al.name,
                    picUrl: item.al.picUrl,
                    artists: '${item.ar.map((a) => a.name).toList().join('/')}',
                  );
                }).toList());
              },
              behavior: HitTestBehavior.opaque,
              child: PlayAllSongsBar(
                bottomRowItemH: _bottomRowItemH,
                count: songsList.length ?? 0,
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  double get minExtent => minHeight + _bottomRowItemH;

  @override
  double get maxExtent => minHeight + 250.w;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
